package com.edu.exception;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "统一响应")
public class R<T> {
    @Schema(description = "响应码")
    Integer code;
    @Schema(description = "响应信息")
    String message;
    @Schema(description = "响应数据")
    T data;

    public static <T> R<T> success(T data) {
        R<T> r = new R<>();
        r.setCode(200);
        r.setMessage("success");
        r.setData(data);
        return r;
    }

    public static <T> R<T> success() {
        return success(null);
    }

    public static <T> R<T> error(String message) {
        R<T> r = new R<>();
        r.setCode(500);
        r.setMessage(message);
        return r;
    }

    public static <T> R<T> error(String message, Integer code) {
        return new R<>(code, message, null);
    }

    public static <T> R<T> error(String message, Integer code, T data) {
        return new R<>(code, message, data);
    }
}
